package pzhu.web;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileSystemView;

import org.apache.http.ParseException;

public class WelcomeInterface {
	static JTextField path = new JTextField();
	public static JTextField number = new JTextField();
	public static JTextField page = new JTextField();
	public static JTextArea log = new JTextArea();
	public static JButton start = new JButton("开始下载");
	static int x = 0;

	public WelcomeInterface() {
		// TODO Auto-generated constructor stub
	}

	public static void show() {
		JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
		jfc.setDialogTitle("选择文件目录保存文件 ");
		jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

		if (jfc.showDialog(null, "选择") == JFileChooser.APPROVE_OPTION) {
			if (jfc.getSelectedFile().isDirectory()) {
				path.setText(jfc.getSelectedFile().getPath());
				x = 1;
			}
		} else if (x == 0) {
			JOptionPane.showMessageDialog(null, "您已退出！", "提示！", JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
	}

	public static void welcome() {
		Choice choice = new Choice();
		final Thread thread = new Thread(choice);
		JFrame jF = new JFrame();
		JMenuBar popup = new JMenuBar();
		JMenu item = new JMenu("重新选择保存路径");
		JMenu item1 = new JMenu("   强制关闭");
		JMenuItem chongxuan = new JMenuItem("重选路径");
		item.add(chongxuan);
		JMenuItem tuichu = new JMenuItem("强制退出！");
		item1.add(tuichu);
		popup.add(item);
		popup.add(item1);
		chongxuan.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				show();
			}
		});
		tuichu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if (JOptionPane.showConfirmDialog(null, "确定强制退出？", "提示！",
						JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					System.exit(0);
				}
			}
		});
		jF.setJMenuBar(popup);
		jF.setTitle("彼岸图网批量下载软件v2.1");
		jF.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width - 350) / 2,
				(Toolkit.getDefaultToolkit().getScreenSize().height - 500) / 2, 350, 500);
		jF.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		path.setBounds(100, 50, 200, 20);
		path.setEditable(false);
		jF.add(path);

		JLabel path = new JLabel();
		path.setText("保存的路径：");
		path.setBounds(25, 50, 100, 20);
		jF.add(path);

		number.setBounds(150, 80, 20, 20);
		number.setDocument(new NumberLenghtLimitedDmt(2));
		jF.add(number, BorderLayout.CENTER);

		JLabel path2 = new JLabel();
		path2.setText("需要下载的图片序号：");
		path2.setBounds(25, 80, 150, 20);
		jF.add(path2);

		page.setBounds(100, 100, 25, 20);
		page.setDocument(new NumberLenghtLimitedDmt(3));
		jF.add(page, BorderLayout.CENTER);

		JLabel path3 = new JLabel();
		path3.setText("下载多少页：        （一页20张图片）");
		path3.setBounds(25, 100, 300, 20);
		jF.add(path3);

		log.setLineWrap(true);
		log.setText("下载日志：\n\r");
		JScrollPane jScrollPane = new JScrollPane(log);
		jScrollPane.setBounds(25, 230, 290, 200);
		log.setEditable(false);
		jF.add(jScrollPane);

		JLabel fengjing = new JLabel();
		fengjing.setText("4K风景 【0】");
		fengjing.setBounds(10, 130, 80, 20);
		jF.add(fengjing);
		JLabel meinv = new JLabel();
		meinv.setText("4K美女 【1】");
		meinv.setBounds(90, 130, 80, 20);
		jF.add(meinv);
		JLabel youxi = new JLabel();
		youxi.setText("4K游戏 【2】");
		youxi.setBounds(170, 130, 80, 20);
		jF.add(youxi);
		JLabel dongman = new JLabel();
		dongman.setText("4K动漫 【3】");
		dongman.setBounds(250, 130, 80, 20);
		jF.add(dongman);
		JLabel yingshi = new JLabel();
		yingshi.setText("4K影视 【4】");
		yingshi.setBounds(10, 170, 80, 20);
		jF.add(yingshi);
		JLabel mingxing = new JLabel();
		mingxing.setText("4K明星 【5】");
		mingxing.setBounds(90, 170, 80, 20);
		jF.add(mingxing);
		JLabel qiche = new JLabel();
		qiche.setText("4K汽车 【6】");
		qiche.setBounds(170, 170, 80, 20);
		jF.add(qiche);
		JLabel dongwu = new JLabel();
		dongwu.setText("4K动物 【7】");
		dongwu.setBounds(250, 170, 80, 20);
		jF.add(dongwu);
		JLabel renwu = new JLabel();
		renwu.setText("4K人物 【8】");
		renwu.setBounds(10, 210, 80, 20);
		jF.add(renwu);
		JLabel meishi = new JLabel();
		meishi.setText("4K美食 【9】");
		meishi.setBounds(90, 210, 80, 20);
		jF.add(meishi);
		JLabel zongjiao = new JLabel();
		zongjiao.setText("4K宗教 【10】");
		zongjiao.setBounds(170, 210, 85, 20);
		jF.add(zongjiao);
		JLabel beijing = new JLabel();
		beijing.setText("4K背景 【11】");
		beijing.setBounds(250, 210, 85, 20);
		jF.add(beijing);
		JLabel ti = new JLabel();
		ti.setFont(new Font("黑体", Font.LAYOUT_LEFT_TO_RIGHT, 20));
		ti.setText("欢 迎 使 用 彼 岸 网 爬 虫 软 件");
		ti.setBounds(10, 10, 350, 20);
		jF.add(ti);
		start.setBounds(230, 80, 100, 40);
		jF.add(start);
		start.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (number.getText().length() <= 0) {//
					JOptionPane.showMessageDialog(null, "请输入选择的序号！", "提示！", JOptionPane.ERROR_MESSAGE);
				} else if (page.getText().length() <= 0) {
					JOptionPane.showMessageDialog(null, "请输入下载的总页数！", "提示！", JOptionPane.ERROR_MESSAGE);
				} else if (Integer.parseInt(number.getText()) > 11 || Integer.parseInt(number.getText()) < 0) {
					JOptionPane.showMessageDialog(null, "请输入正确的序号！", "错误！", JOptionPane.ERROR_MESSAGE);
				} else if (Integer.parseInt(page.getText()) == 0) {
					JOptionPane.showMessageDialog(null, "请输入正确的页数！", "错误！", JOptionPane.ERROR_MESSAGE);
				} else {
					try {
						start.setSelected(false);
						number.setEditable(false);
						page.setEditable(false);
						Choice.index = number.getText();
						thread.start();
					} catch (ParseException e1) {
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null, "网络连接异常（404）！", "错误！", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		jF.setLayout(null);
		jF.setVisible(true);
	}
}
